package cn.mcch.investigator.dialog;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import java.util.Arrays;

import cn.mcch.investigator.R;
import cn.mcch.investigator.adapter.base.CommonAdapter;
import cn.mcch.investigator.adapter.base.ViewHolder;
import cn.mcch.investigator.application.InvestigatorApplication;
import cn.mcch.investigator.listener.OnCommonClickListener;
import cn.mcch.investigator.utils.DialogUtils;

/**
 * Created by Administrator on 2016/12/19.
 */
public class CarCodeDialog extends BaseDialog implements View.OnClickListener, AdapterView.OnItemClickListener {

    private OnCommonClickListener l;
    private CommonAdapter adapter;
    private String[] data;
    private String[] dataCarArea;
    private String[] dataCarArmy;
    private int numColumns;
    private CarAreaArmyDialog carAreaArmyDialog;
    private int car_code = 1;
    private int car_area = 2;
    private int car_army = 3;

    private int car = car_code;

    @Override
    protected void initEventListener() {
        ((GridView) findViewById(R.id.gv_car_code)).setOnItemClickListener(this);
        findViewById(R.id.sb_delete).setOnClickListener(this);
        findViewById(R.id.sb_area).setOnClickListener(this);
        findViewById(R.id.sb_army).setOnClickListener(this);
        findViewById(R.id.sb_cancel).setOnClickListener(this);
        setOnClickListener(l, R.id.sb_confirm);
    }

    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container) {
        return View.inflate(InvestigatorApplication.getInstance(), R.layout.dialog_car_code, null);
    }

    @Override
    public void initData() {
        dataCarArea = getResources().getStringArray(R.array.car_area);
        dataCarArmy = getResources().getStringArray(R.array.car_army);
        ((GridView) findViewById(R.id.gv_car_code)).setNumColumns(numColumns);
        adapter = new CommonAdapter<String>(InvestigatorApplication.getInstance(), R.layout.list_item_car_area_army, Arrays.asList(data)) {
            @Override
            protected void convert(ViewHolder viewHolder, String item, int position) {
                viewHolder.setText(R.id.tv_car_area_army, item);
            }
        };
        setAdapterViewAdapter(R.id.gv_car_code, adapter);
    }

    public CarCodeDialog setOnCommonClickListener(OnCommonClickListener l) {
        this.l = l;
        return this;
    }

    public CarCodeDialog setData(String[] data) {
        this.data = data;
        return this;

    }

    public CarCodeDialog setNumColumns(int numColumns) {
        this.numColumns = numColumns;
        return this;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.sb_delete:
                // 删除
                String value = getViewText(R.id.sb_car_code);
                if (!TextUtils.isEmpty(value)) {
                    setText(R.id.sb_car_code, value.substring(0, value.length() - 1));

                }
                break;
            case R.id.sb_area:
                // 地区
                carAreaArmyDialog = DialogUtils.showCarAreaArmyDialog(getActivity().getSupportFragmentManager(), this, dataCarArea, 7);
                car = car_area;
                break;
            case R.id.sb_army:
                // 军警
                car = car_army;
                carAreaArmyDialog = DialogUtils.showCarAreaArmyDialog(getActivity().getSupportFragmentManager(), this, dataCarArmy, 7);
                break;
            case R.id.sb_cancel:
                // 取消
                dismiss();
                break;
            default:
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String value = getViewText(R.id.sb_car_code);
        String append = null;
        if (car == car_code) {
            append = data[position];
        } else if (car == car_area) {
            carAreaArmyDialog.dismiss();
            car = car_code;
            append = dataCarArea[position];
        } else if (car == car_army) {
            carAreaArmyDialog.dismiss();
            car = car_code;
            append = dataCarArmy[position];
        }
        setText(R.id.sb_car_code, value.concat(append));
    }

    public String getCarCode(){
        return getViewText(R.id.sb_car_code);
    }
}
